<?php
    /**
     *	Row class
     *	Provides a representation of a row of a database table and holds relevant metadata.
     *	@author	Simon Copsey <simon.copsey@gmail.com>
     *	@version 0.1
     *	@package data
     */
    class Row {
        private $rowData = array();
        private $isModified = false;
        private $isDeleted = false;
        private $isNew = false;
        
        /**
        * Creates a new row instance. Presumes row requires insertion into
        * database (unless populate() is called subsequently).
        *
        * @access public
        */      
        public function __construct() {
            $this->setNew();
        }

        /**
        * Populates row with the contents of the given array. Presumes the
        * given array is from database and hence do not require insertion.
        *
        * @param Object[] $aRowData Array from which to populate row
        * @param boolean $anIsNewFlag Is the row new and requiring insertion?
        * @access public
        */        
        public function populate($aRowData) {
            $this->updateRow($aRowData);
            $this->resetFlags();
        }
        
        /**
        * Reset the deleted, modified and new flags for the row.
        *
        * @access private
        */
        private function resetFlags() {
            $this->isDeleted = false;
            $this->isModified = false;
            $this->isNew = false;
        }
        
        /**
        * Set the new flag for the row.
        *
        * @param boolean $anIsNewFlag is row marked as new and requiring insertion?
        * @access private
        */
        private function setNew() {
            $this->isDeleted = false;
            $this->isModified = false;
            $this->isNew = true;
        }
        
        /**
        * Get the new flag for the row.
        *
        * @return boolean is row marked as new and requiring insertion?
        * @access public
        */
        public function isNew() {
            return $this->isNew;
        }
        
        /**
        * Set the deleted flag for the row.
        *
        * @access public
        */
        public function setDeleted() {
            $this->isModified = false;
            $this->isNew = false;
            $this->isDeleted = true;
        }
        
        /**
        * Get the deleted flag for the row.
        *
        * @return boolean is row marked as deleted?
        * @access public
        */
        public function isDeleted() {
            return $this->isDeleted;
        }
        
        /**
        * Get the value at the given index.
        *
        * @param Integer $anIndex index of value to be returned
        * @return Object value at given index
        * @access public
        */
        public function getValue($anIndex) {
            return $this->rowData[$anIndex];
        }
        
        /**
        * Get all row values as an ordered array.
        *
        * @return Object[] all row values
        * @access public
        */
        public function getValues() {
            return $this->rowData;
        }
        
        /**
        * Update the value at the given index.
        *
        * @param Integer $anIndex index of value to be updated
        * @param Object $aValue new value
        * @access public
        */
        public function updateValue($anIndex, $aValue) {
            $this->rowData[$anIndex] = $aValue;
            $this->setModified();
        }
        
        /**
        * Update all values for entire row.
        *
        * @param Object $aRowData new data for row,  (must be MYSQL_NUMified)
        * @access public
        */
        public function updateRow($aRowData) {
            $this->rowData = $aRowData;
            $this->setModified();
        }
        
        /**
        * Set the modified flag for the row.
        *
        * @param boolean $anIsModifiedFlag is row marked as new and requiring insertion?
        * @access private
        */
        private function setModified() {
            $this->isDeleted = false;
            if (!$this->isNew) {
                $this->isModified = true;
            }
        }
        
        /**
        * Get the modified flag for the row.
        *
        * @return boolean is row marked as modified?
        * @access public
        */
        public function isModified() {
            return $this->isModified;
        }
    }
?>